﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFPrototypeGridRow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPMODELLib;
using EMPTYPELIBRARYLib;
using EngineeringInternalExtension;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFPrototypeGridRow : CApMFLocatableGridRow
{
  private ITxPlanningObject m_planningObject;
  private TxTransformation m_intermediateUnitPlacementPoint;
  private TxTransformation m_placementOrigin;
  private CApMFPrototypeGridRow.PrototypeTypeEnum m_type;
  private string m_path = "";

  internal CApMFPrototypeGridRow(int prototypeId, int instanceId, int loadedId)
    : base(prototypeId, instanceId, loadedId)
  {
    ITxPlanningObject objectByInternalId = (TxApplication.ActiveDocument.PlatformGlobalServicesProvider as TxEmsGlobalServicesProvider).GetObjectByInternalId(prototypeId);
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    this.m_planningObject = objectByInternalId;
    this.SetIcon();
    this.SetType();
    this.LoadPlacementOrigin();
    this.LoadIntermediatePlacementPoint();
  }

  internal override int SaveNodeToEms(EmpObjectKey parent)
  {
    this.ValidateInternalIds();
    ITxEmsServicesProvider servicesProvider = (ITxEmsServicesProvider) this.m_planningObject.PlatformServicesProvider;
    if (parent.objectId != 0 && (this.m_instanceId == 0 || !CApMFAdminUtils.IsDescendant(parent.objectId, this.m_instanceId)))
    {
      EmpObjectKey empObjectKey;
      empObjectKey.objectId = servicesProvider.InternalId;
      CApMFAdminUtils.ConnectEmsObject(empObjectKey, parent);
      this.m_instanceId = CApMFAdminUtils.GetInstanceId(parent, empObjectKey);
    }
    this.SaveIntermediatePlacementPoint();
    this.SavePlacementOrigin();
    this.SaveAttributes();
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    return servicesProvider.InternalId;
  }

  private void SaveIntermediatePlacementPoint()
  {
    if (this.m_instanceId <= 0)
      return;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    IEmpObject iempObject = (IEmpObject) new EmpObjectClass();
    EmpObjectKey empObjectKey = new EmpObjectKey();
    empObjectKey.objectId = this.m_instanceId;
    if (TxTransformation.op_Inequality(this.m_intermediateUnitPlacementPoint, (TxTransformation) null))
    {
      double[] numArray = new double[6]
      {
        this.m_intermediateUnitPlacementPoint.Translation.X,
        this.m_intermediateUnitPlacementPoint.Translation.Y,
        this.m_intermediateUnitPlacementPoint.Translation.Z,
        this.m_intermediateUnitPlacementPoint.RotationRPY_XYZ.X,
        this.m_intermediateUnitPlacementPoint.RotationRPY_XYZ.Y,
        this.m_intermediateUnitPlacementPoint.RotationRPY_XYZ.Z
      };
      iempObject.SetField(ref empContext, ref empObjectKey, "IntermediateUnitPlacementPoint", (object) numArray);
    }
    else
    {
      double[] numArray = new double[0];
      iempObject.SetField(ref empContext, ref empObjectKey, "IntermediateUnitPlacementPoint", (object) numArray);
    }
  }

  private void SavePlacementOrigin()
  {
    if (this.m_instanceId <= 0)
      return;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    IEmpObject iempObject = (IEmpObject) new EmpObjectClass();
    EmpObjectKey empObjectKey = new EmpObjectKey();
    empObjectKey.objectId = this.m_instanceId;
    if (TxTransformation.op_Inequality(this.m_placementOrigin, (TxTransformation) null))
    {
      double[] numArray = new double[6]
      {
        this.m_placementOrigin.Translation.X,
        this.m_placementOrigin.Translation.Y,
        this.m_placementOrigin.Translation.Z,
        this.m_placementOrigin.RotationRPY_XYZ.X,
        this.m_placementOrigin.RotationRPY_XYZ.Y,
        this.m_placementOrigin.RotationRPY_XYZ.Z
      };
      iempObject.SetField(ref empContext, ref empObjectKey, "PlacementOrigin", (object) numArray);
    }
    else
    {
      double[] numArray = new double[0];
      iempObject.SetField(ref empContext, ref empObjectKey, "PlacementOrigin", (object) numArray);
    }
  }

  private void LoadIntermediatePlacementPoint()
  {
    if (this.m_instanceId <= 0)
      return;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
    {
      objectId = this.m_instanceId
    }, "IntermediateUnitPlacementPoint");
    if (field.Value == null)
      return;
    double[] numArray = (double[]) field.Value;
    if (numArray.Length != 6)
      return;
    this.m_intermediateUnitPlacementPoint = new TxTransformation(new TxVector(numArray[0], numArray[1], numArray[2]), new TxVector(numArray[3], numArray[4], numArray[5]), (TxTransformation.TxRotationType) 1);
  }

  private void LoadPlacementOrigin()
  {
    if (this.m_instanceId <= 0)
      return;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpFieldInfo field = ((IEmpObject) new EmpObjectClass()).GetField(ref empContext, ref new EmpObjectKey()
    {
      objectId = this.m_instanceId
    }, "PlacementOrigin");
    if (field.Value == null)
      return;
    double[] numArray = (double[]) field.Value;
    if (numArray.Length != 6)
      return;
    this.m_placementOrigin = new TxTransformation(new TxVector(numArray[0], numArray[1], numArray[2]), new TxVector(numArray[3], numArray[4], numArray[5]), (TxTransformation.TxRotationType) 1);
  }

  private void SetIcon()
  {
    TxEmsServicesProvider servicesProvider1 = this.m_planningObject.PlatformServicesProvider as TxEmsServicesProvider;
    TxTcServicesProvider servicesProvider2 = this.m_planningObject.PlatformServicesProvider as TxTcServicesProvider;
    if (servicesProvider1 != null)
    {
      this.m_icon = servicesProvider1.Icon;
    }
    else
    {
      if (servicesProvider2 == null)
        return;
      this.m_icon = ((TxTcAnyServicesProvider) servicesProvider2).Icon;
    }
  }

  private void SetType()
  {
    if (this.m_planningObject.IsDerivedFromPlanningType("UnitConfigurationPrototype"))
      this.m_type = CApMFPrototypeGridRow.PrototypeTypeEnum.Unit;
    else
      this.m_type = CApMFPrototypeGridRow.PrototypeTypeEnum.Resource;
  }

  internal override string Name
  {
    get => TxObjectEx.GetPlanningObjectName((ITxObject) this.m_planningObject);
  }

  internal CApMFPrototypeGridRow.PrototypeTypeEnum Type
  {
    get => this.m_type;
    set => this.m_type = value;
  }

  internal ITxPlanningObject Prototype => this.m_planningObject;

  internal string PrototypePath
  {
    get
    {
      if (this.m_path == "")
      {
        EmpContext empContext;
        empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
        TxEmsServicesProvider servicesProvider = this.m_planningObject.PlatformServicesProvider as TxEmsServicesProvider;
        EmpToolPrototypeClass toolPrototypeClass = new EmpToolPrototypeClass();
        EmpObjectKey empObjectKey;
        empObjectKey.objectId = servicesProvider.InternalId;
        EmpObjectKey threeDrep = toolPrototypeClass.GetThreeDRep(ref empContext, ref empObjectKey);
        this.m_path = new EmpThreeDRepClass()[ref empContext, ref threeDrep];
      }
      return this.m_path;
    }
    set => this.m_path = value;
  }

  internal TxTransformation IntermediateUnitPlacementPoint
  {
    get => this.m_intermediateUnitPlacementPoint;
    set => this.m_intermediateUnitPlacementPoint = value;
  }

  internal TxTransformation PlacementOrigin
  {
    get => this.m_placementOrigin;
    set => this.m_placementOrigin = value;
  }

  internal enum PrototypeTypeEnum
  {
    Resource,
    Unit,
  }
}
